package com.lab.project.visitor.verification.callback;

import com.lab.common.utils.DateUtils;
import com.lab.common.utils.MyServerConfig;
import com.lab.framework.config.LabConfig;
import com.lab.project.visitor.domain.vo.VisitorRecordVO;
import com.lab.project.visitor.record.enums.ErrorType;
import com.lab.project.visitor.record.service.IVisitorService;
import com.lab.project.visitor.verification.utils.Base64Save;
import com.neusiri.ai.api.yitu.verification.callback.entity.VerificationIdInfo;
import com.neusiri.ai.api.yitu.verification.callback.params.VerificationCallbackParams;
import com.neusiri.ai.api.yitu.verification.callback.runner.VerificationCallbackRunner;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * 人证核验机的回调
 *
 * @author 黄晏凯
 */
@Component
@Slf4j
@AllArgsConstructor
public class VerificationCallback implements VerificationCallbackRunner {

    private static final String BASE_URL="/profile/upload/";

    private IVisitorService visitorService;

    private MyServerConfig serverConfig;

    @Override
    public void execute(VerificationCallbackParams params) {
        //判断是否是同一个人
        if (params.getData().getVerifyResult().getSamePerson()) {
            VisitorRecordVO visitorRecordVO = new VisitorRecordVO();
            //通过idInfo获取身份信息
            VerificationIdInfo idInfo = params.getData().getIdInfo();
            visitorRecordVO.setUserIdCard(idInfo.getIdNum());
            visitorRecordVO.setUserName(idInfo.getName());
            visitorRecordVO.setUserSex("男".equals(idInfo.getSex()) ? "0" : "1");
            visitorRecordVO.setUserPhone("");
            //人脸图片
            String imageStr = params.getData().getVerifyResult().getFaceImage();
            //文件名
            String faceName = Base64Save.extractFilename("personFace.jpg");
            //上传地址
            String url = LabConfig.getUploadPath();
            boolean result = Base64Save.saveImage(imageStr,url, faceName);
            log.info("=========={}=======",result);
            if (result) {
                //图片路径
                String realUrl= serverConfig.getUrl()+BASE_URL+faceName;
                visitorRecordVO.setFacePicUrl(realUrl);
                visitorRecordVO.setCreateBy("认证核验机");
                visitorRecordVO.setUpdateBy("认证核验机");
                visitorRecordVO.setVisitStatus("0");
                visitorRecordVO.setVisitStartTime(DateUtils.getNowDate());
                String res = visitorService.saveVisitor(visitorRecordVO);
                log.info("认证核验机插入的结果:{}",ErrorType.getMessageByCode(res));
            }
        }


    }
}
